<!--
 * @Author       : wfl
 * @LastEditors: ww
 * @description  :
 * @updateInfo   :
 * @Date         : 2022-11-23 15:50:42
 * @LastEditTime: 2023-06-30 15:48:58
-->
<script lang="ts" setup name="Dispose">
import Node from '@main/views/approve-workflow/WorkFlow/tree-node/Node.vue'
import { computed, reactive } from 'vue'
import { EApproveCategory } from '@main/views/approve-workflow/enum'

const props = defineProps({
  config: {
    type: Object,
    default: () => {
      return {}
    }
  }
})

const emits = defineEmits<{
  (e: 'selected'): void
  (e: 'insertNode', val: string): void
  (e: 'delNode', node: any): void
}>()

const state = reactive({
  showError: false,
  errorInfo: ''
})

const content = computed(() => {
  const config = props.config.props
  switch (config?.approvalCategory) {
    case EApproveCategory.指定成员:
      if (config.assignedUser.length > 0) {
        const texts: any[] = []
        config.assignedUser.forEach((org: { elementName: string; }) =>
          texts.push(org.elementName)
        )
        return String(texts).replaceAll(',', '、')
      }
      else {
        return '请指定办理人'
      }
    case EApproveCategory.发起人自己:
      return '发起人自己'
    case EApproveCategory.发起人自选:
      return config.selfSelect.multiple ? '发起人自选多人' : '发起人自选一人'
    case EApproveCategory.发起人部门主管:
      return config.leader.level > 1
        ? `发起人的第 ${config.leader.level} 级主管`
        : '发起人的直接主管'
    case EApproveCategory.指定角色:
      if (config.assignedUser.length > 0) {
        return `指定角色（${String(config.assignedUser.map((c: any) => c.elementName)).replaceAll(
          ',',
          '、'
        )}）`
      }
      else { return '指定角色（未设置）' }

    default:
      return '未知设置项'
  }
})

// const getFormItemById = (id: string) => {
//   return (
//     flowStore.design?.formItems?.find(
//       (item: { id: string }) => item.id === id
//     ) || null
//   );
// };
// 校验数据配置的合法性
const validate = (err: string[]) => {
  try {
    return (state.showError
      = !validated[`${props.config.props.approvalCategory as keyof typeof validated}`](err))
  }
  catch (e: any) {
    return true
  }
}

const validated = {
  SPECIFIED_USER: (err: string[]) => {
    if (props.config.props.assignedUser.length > 0) {
      return true
    }
    else {
      state.errorInfo = '请指定办理人员'
      err.push(`${props.config.name} 未指定办理人员`)
      return false
    }
  },
  INITIATOR_SPECIFIED: () => {
    return true
  },
  INITIATOR_DEPT_MANAGER: () => {
    return true
  },
  LEADER: () => {
    return true
  },
  ROLE: (err: string[]) => {
    if (props.config.props.assignedUser.length <= 0) {
      state.errorInfo = '请指定负责审批的系统角色'
      err.push(`${props.config.name} 未指定审批角色`)
      return false
    }
    return true
  },
  INITIATOR_SELF: () => {
    return true
  },
  REFUSE: () => {
    return true
  }
}

const handSelected = () => {
  emits('selected')
}
const handInsertNode = (type: string) => {
  emits('insertNode', type)
}
const handDelNode = () => {
  emits('delNode', props?.config)
}

defineExpose({ validate })
</script>

<template>
  <Node
    :title="config.name"
    :show-error="state.showError"
    :content="content"
    :error-info="state.errorInfo"
    placeholder="请设置办理人"
    header-bgc="#F56C6C"
    header-icon="ikflow-shenhe"
    @selected="handSelected"
    @delNode="handDelNode"
    @insertNode="handInsertNode"
  />
</template>
